<template>
  <transition name="slide">
    <music-list :title="title" :bg-image="bgImage" :songs="songs"></music-list>
  </transition>
</template>

<script>
import MusicList from '../music-list/MusicList'
import { mapGetters } from 'vuex'
import { getSongList, getSongsUrls } from '../../api/index'
import { createSong } from '../../assets/js/song'

export default {
  name: 'Disc',
  data() {
    return {
      songs: []
    }
  },
  computed: {
    title() {
      return this.disc.name
    },
    bgImage() {
      return this.disc.picUrl
    },
    ...mapGetters([
      'disc'
    ])
  },
  created() {
    this._getSongList()
  },
  methods: {
    _getSongList() {
      if (!this.disc.id) {
        this.$router.push('/recommend')
        return
      }
      getSongList({ id: this.disc.id }).then(res => {
        if (res.status === 200) {
          this.songs = this._normalizeSongs(res.data.playlist.tracks)
        }
        const songIds = []
        this.songs.forEach(song => {
          songIds.push(song.id)
        })
        return songIds
      }).then(songIds => {
        return getSongsUrls({ id: songIds.join(',') })
      }).then(urls => {
        console.log(urls)
        // 我貌似只能点击一首去请求一首歌的url了
        const urlsData = urls.data.data
        console.log(urlsData.length)
        for (let i = 0; i < urlsData.length; i++) {
          this.songs.forEach(song => {
            console.log('怎么说')
            if (song.id === urlsData[i].id) {
              song.url = urlsData[i].url
            }
          })
        }
      }).catch(err => {
        console.log(err)
      })
    },
    _normalizeSongs(list) {
      const ret = []
      list.forEach(musicData => {
        if (musicData.id) {
          ret.push(createSong(musicData))
        }
      })
      return ret
    }
  },
  components: {
    MusicList
  }
}
</script>

<style lang="scss" scoped>
.slide-enter-active, .slide-leave-active{
  transition: all 0.3s;
}
.slide-enter, .slide-leave-to{
  transform: translate3d(100%, 0, 0);
}
</style>